$(function(){
	$("#main_content").load("../procedure/proceManage.html");
});


/**
 * 首页控制JS
 * 
 */
var Index_menu = function() {
	
	/**
	 * 顶部topDiv
	 */
	this.topDiv = null;
	
	/**
	 * 头部+导航(一级菜单目录)
	 */
	this.firstMenuDiv = null;
	
	/**
	 * 二、三级菜单div
	 */
	this.secondMenuDiv = null;
	
	/**
	 * table页menuPanelDiv
	 */
	this.menuPanelDiv = null;
	// panelFrame的组件对象
	this.panelFrame = null;
	
	/**
	 * 主页面mainDiv
	 */
	this.mainDiv = null;
		
	/**
	 * 登陆用户的基本信息
	 */
	this.userInfo = null;
		
	/**
	 * 菜单数据集合
	 */
	this.menuData = null;
	
	/**
	 * 存放所有一级菜单的Array集合
	 */
	this.firstMenuArray = new Array();

	/**
	 * 存放所有一级菜单下面的子菜单的HTML(key=firstPrivilegeId;value=HTML)
	 */
	this.firstChildHtml = new Object();

	/**
	 * 存放所有1、2、3三级菜单所有节点(key=PrivilegeId;value=menuObject)
	 */
	this.allMenuObject = new Object();
	
	/**
	 * 最后一次移动鼠标的时间
	 */		
	this.lastMoveTime = (new Date()).getTime();
	
	/**
	 * 定时器句柄
	 */
	this.intervalTime = null;
	
	/**
	 * 异步查询当前登陆用户的数据（包括登陆的用户基本信息、菜单）
	 */
	this.ajaxFindUserData = function() {
		// 封装请求数据对象
		/*var requestObj = new Object();
		requestObj['url'] = '/portal/menu/queryMenu.json';
		var responseObj = Dic.Ajax.request(requestObj);
		if(null != responseObj && '0000' == responseObj.code) {
			this.userInfo = responseObj.data.userInfo;
			this.menuData = responseObj.data.menuData;
		}*/
		/*this.userInfo = new Object();
		this.userInfo.STAFF_NAME = '张三';
		this.userInfo.ORG_NAME = '深圳万科物业';
		this.userInfo.SERVICE_NAME = '深圳金域蓝花物业服务中心';
		this.userInfo.LOGIN_TIME = '2015-01-25 17:06:52';*/
		this.userInfo = getCurrLoginUser();
		this.menuData = [];
		this.menuData.push({'id':'0','power':'1','url':'../user/userManager.html','title':'用户信息管理'});
		this.menuData.push({'id':'1','power':'1','url':'../procedure/proceManage.html','title':'工序明细管理'});
		this.menuData.push({'id':'2','power':'0','url':'../salary/salaryManager.html','title':'工资明细管理'});
	}

	/**
	 * 写topDiv域下面的用户信息
	 */
	this.writeTopDiv = function() {
		if(null != this.userInfo) {
			this.topDiv.find("#userInfoDiv").html(
				'<em class="cBlue">&nbsp;' +processUndefined(this.userInfo.real_name)+ 
				'</em>，您好！' + getNowDate());
		}
		// 退出事件加载
		this.topDiv.find('#logoutA').unbind().on('click', this.logout);
	}
	
	/**
	 * 初始化一级菜单
	 */
	this.initFirstMenu = function() {
		if(null != this.menuData && this.menuData.length > 0) {
			var firstMenuIndex = 0;
			for(var i = 0 ; i < this.menuData.length ; i ++ ) {
				var tMenu = this.menuData[i];
				if(tMenu.power == 1) {
					this.firstMenuArray[firstMenuIndex] = tMenu;
//					this.firstChildHtml[parseInt(tMenu.PRIVILEGE_ID)] = -1;
					this.allMenuObject[parseInt(tMenu.id)] = tMenu;
					firstMenuIndex ++ ;
				} else if (tMenu.power == 0){
					if(this.userInfo.type==0){
						this.firstMenuArray[firstMenuIndex] = tMenu;
						this.allMenuObject[parseInt(tMenu.id)] = tMenu;
						firstMenuIndex ++ ;
					}
				}
			}
			this.writeFirstMenu();
		}
	}

	/**
	 * 写firstMenuDiv域下面的一级菜单
	 */
	this.writeFirstMenu = function() {
		// STEP-1 获取1级菜单的html
		var writeHTML = '';
		for(var i = 0 ; i < this.firstMenuArray.length ; i ++ ) {
			var tMenu = this.firstMenuArray[i];
			/*writeHTML += '<dl firstMenuDl="' +tMenu.PRIVILEGE_ID+ '" >'
	        	+ '<dt><img src="' +tMenu.IMAGE_DIS+ '" /></dt>'
	        	+ '<dd>' +tMenu.PRIVILEGE_NAME+ '</dd>'
	        	+ '</dl>';*/
			writeHTML += '<li><a href="#" id="'+tMenu.id+'">'+tMenu.title+'</a></li>'
		}
		// STEP-2 显示html
		this.firstMenuDiv.html(writeHTML);
		// STEP-3 注入事件
		var thisObj = this;
		// 一级菜单鼠标移入样式
		/*this.firstMenuDiv.find('dl[firstMenuDl]').mouseenter(	// 鼠标移入移出样式
			function(){
		    	//鼠标移入
				thisObj.secondMenuDiv.hide();
		    	var firstPrivilegeId = $(this).attr("firstMenuDl");
		    	if(thisObj.initSecondMenu(firstPrivilegeId) && firstPrivilegeId!="1000" && firstPrivilegeId!="4000" ) {	// 2、3级菜单有数据才显示 如果是"客户服务", "综合查询"则不加载
		    		thisObj.secondMenuShow($(this));
		    	}
			}
		);*/
		// 一级菜单鼠标点击事件
		this.firstMenuDiv.find('li a[id]').click(function() {	// 鼠标点击事件
	    	var id = $(this).attr("id");
	    	var ttMenuObject = thisObj.allMenuObject[parseInt(id)];
	    	/*if('newWin' == ttMenuObject.MENU_TARGET ) {
	    		window.open(ttMenuObject.URL,ttMenuObject.PRIVILEGE_NAME,'top=0, left=0, toolbar=yes, menubar=yes, scrollbars=yes, resizable=yes, location=yes, status=yes');
	    	} else if('mainFrm' == ttMenuObject.MENU_TARGET ) {*/
	    		thisObj.onloadChildSystemPage(ttMenuObject);
//	    	}
		});
		// 2、3级菜单鼠标移出样式
		/*this.secondMenuDiv.mouseleave(	// 移出样式
			function(){
		    	//鼠标移出
		    	thisObj.secondMenuDiv.hide(200);
			}
		);*/
		// 鼠标移入main主页面后隐藏2、3级菜单
		/*this.mainDiv.mouseenter(
			function(){
		    	//鼠标移出
		    	thisObj.secondMenuDiv.hide(200);
			}
		);*/
	}
	
	/**
	 * 二、三级菜单的展示和展示位置
	 */
	this.secondMenuShow = function(firstMenuElement) {
		var firstMenuLeft = firstMenuElement.offset().left;
		var windowWidth = $(window).width(); 
		var secondMenuWidth = this.secondMenuDiv.width();
		var secondMenuTop = firstMenuElement.offset().top + firstMenuElement.height();
		var secondMenuLeft = firstMenuLeft;
		var secondMenuRight = firstMenuLeft + secondMenuWidth;
		if(secondMenuLeft < 0) {
			secondMenuLeft = 0;
		}
		if(secondMenuRight + 24 >= windowWidth ) {
			secondMenuLeft = windowWidth - secondMenuWidth - 24;
			secondMenuRight = windowWidth - 24;
		}	
		this.secondMenuDiv.css({
			top:secondMenuTop,
			left:secondMenuLeft,
			width: secondMenuWidth
		});
		
		this.secondMenuDiv.show();
	}
	
	/**
	 * 初始化二、三级菜单
	 */
	this.initSecondMenu = function(firstPrivilegeId) {
		// STEP-1 获取2、3级菜单的html
		var tHtml = this.firstChildHtml[parseInt(firstPrivilegeId)];
		if(-1 == tHtml) {
			tHtml = this.joinMenu(firstPrivilegeId);
		}
		// STEP-2 显示html
		this.secondMenuDiv.html(tHtml);
		if('' != tHtml) {
			// STEP-3 注入事件
			var thisObj = this;
			this.secondMenuDiv.find('a[menuA]').click(function() {
		    	var ttPrivilegeId = $(this).attr("menuA");
		    	var ttMenuObject = thisObj.allMenuObject[parseInt(ttPrivilegeId)];
		    	if('newWin' == ttMenuObject.MENU_TARGET ) {
		    		window.open(ttMenuObject.URL,ttMenuObject.PRIVILEGE_NAME,'top=0, left=0, toolbar=yes, menubar=yes, scrollbars=yes, resizable=yes, location=yes, status=yes');
		    	} else if('mainFrm' == ttMenuObject.MENU_TARGET) {
		    		thisObj.onloadChildSystemPage(ttMenuObject);
		    	}
			});
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 组合2、3级菜单
	 * return 返回组装好的html
	 */
	this.joinMenu = function(firstPrivilegeId) {
		var firstHtml = '';
		// STEP-1 获取该一级节点下所有二三级菜单
		for(var i = 0 ; i < this.menuData.length ; i ++ ) {
			var tMenu2 = this.menuData[i];
			if( (tMenu2.PARENT_PRIVILEGE_ID + '') == firstPrivilegeId ) {
				this.allMenuObject[parseInt(tMenu2.PRIVILEGE_ID)] = tMenu2;
				// STEP-2 拼接2、3级菜单和保存3级菜单的数据
				firstHtml += '<ul>';
				firstHtml += '<li><a menuA="' +tMenu2.PRIVILEGE_ID+ '" class="a_h3">' +tMenu2.PRIVILEGE_NAME+ '</a></li>';
				firstHtml += '<li>';
				for(var j = i+1 ; j < this.menuData.length ; j ++ ) {
					var tMenu3 = this.menuData[j];
					if( tMenu3.PARENT_PRIVILEGE_ID == tMenu2.PRIVILEGE_ID ) {
						// STEP-3保存3级菜单的数据
						this.allMenuObject[parseInt(tMenu3.PRIVILEGE_ID)] = tMenu3;
						firstHtml += '<a menuA="' +tMenu3.PRIVILEGE_ID+ '" href="#">' +tMenu3.PRIVILEGE_NAME+ '</a>';
					}
				}
				firstHtml += '</li>';
				firstHtml += '</ul>';
			}
		}
		this.firstChildHtml[parseInt(firstPrivilegeId)] = firstHtml;
		return firstHtml;
	}
	
	/**
	 * 加载各个集成子系统的页面
	 */
	this.onloadChildSystemPage = function(ttMenu) {
		var timstamp = new Date().valueOf();
		var url = ttMenu.url;
		if(url) {
			/*if (url.indexOf("?")>=0){  
				url = url + "&t=" + timstamp;   
			}else {  
				url = url + "?t=" + timstamp;  
			}*/
			this.showMainFrmPage( ttMenu.title, url);
		}
	}
	
	/**
	 * mainDiv中展示页面
	 */
	this.showMainFrmPage = function(name,url) {
		// STEP-0 初始化mainFrame
		if(null == this.panelFrame) {
			this.initPanelPage();
		}
		// STEP-1 查询该URL是否已经打开
		var ttIndex = this.panelFrame.getFrameNumFun(name);
		if(null == ttIndex) {
			// STEP-1.1 增加新的panel并显示等操作
			if(this.panelFrame.getLiLength() >= 8) {	// table page max is 8 .
				art.dialog.message('warning','不能超过8个标签页！',null);
				return;
			} else {
				var liDataLength = this.panelFrame.getLiDataLength();
				var frameIndex = liDataLength == 0 ? 0 : liDataLength ;
				// STEP-2 动态增加mainFrame
				this.mainDiv.append('<iframe id="mainFrm' +frameIndex+ '" name="mainFrm' +frameIndex+ '" frameborder="0" scrolling="auto" style="display: none;"></iframe>');
				// STEP-3 动态增加mainFrame的样式
				this.setMainFrmLayout(frameIndex);
				// STEP-4 添加到panel table 中
				var panelIndex = this.panelFrame.addLiFun({
					title : name , 
					content : this.mainDiv.find('#mainFrm' + frameIndex ) ,
					url : url , 
					closeable : true 
				});
				// STEP-5 最后展示这个刚刚添加的panel page
				this.panelFrame.showLiFun(panelIndex);
			}
		} else {
			// STEP-1.2 展示这个刚刚添加的panel page
			this.panelFrame.showLiFun(ttIndex);
		}
	}

	/**
	 * 初始化PanelPage的展示区域
	 */
	this.initPanelPage = function() {
		this.menuPanelDiv.show();
		this.mainDiv.html('');
		this.panelFrame = new Custom_tabpanel();
		this.panelFrame.init({
	    	headDiv : this.menuPanelDiv, // 每次加载必填
	    	config : {
				closeable : true,
				panelType : 'iframe',
				asyn : true,
				showNum : 0
	    	}
	    });
	}

	/**
	 * 初始化指定的mainFrame的展示区域
	 */
	this.setMainFrmLayout = function(frameIndex) {
		var mainTop = this.mainDiv.offset().top;
		var windowHeight = $(document).height();
		var windowWidth = '100%'; 
/*		var windowHeight = $(window).height();
		var windowWidth = $(window).width(); 
*/		this.mainDiv.find('#mainFrm'+frameIndex).css({
			width: windowWidth,
			height: windowHeight - mainTop - 8,	// 8是一些线条的的宽度
			display : 'none'
		});
	}
	
	/**
	 * 进入首页
	 */
	this.gotoMainPage = function() {
		var portalFirstUrl = '../procedure/proceManage.html';
		//定义管理员或员工的首页，现都为工序明细管理
		/*if(this.userInfo.type==0) {
			portalFirstUrl = '';
		} else{
			
		}*/
//		var timstamp = new Date().valueOf();
//		if (portalFirstUrl.indexOf("?")>=0){  
//			portalFirstUrl = portalFirstUrl + "&t=" + timstamp;   
//		}else {  
//			portalFirstUrl = portalFirstUrl + "?t=" + timstamp;  
//		}
		this.menuPanelDiv.hide();
		this.menuPanelDiv.html('');
		this.panelFrame = null;
		this.mainDiv.html('<iframe id="mainFrm" name="mainFrm" frameborder="0" scrolling="auto" style="display: none;"></iframe>');
		this.setMainFrmLayout('');
		this.mainDiv.find('#mainFrm').show();
		this.mainDiv.find('#mainFrm').attr('src', portalFirstUrl);
	}
	
	/**
	 * 进入我的设置
	 */
	this.gotoMySetPage = function() {
		art.dialog.open("/portal/myset/changePassword.html", {id:'changePassword',title:'我的设置',lock:true,width:'500px',height:'300px'}, false);
	}
	
	/**
	 * 关闭我的设置
	 */
	this.closeSetPage = function() {
		art.dialog({id:"changePassword"}).close();
	}
	
	/**
	 * 退出
	 */
	this.logout = function() {
		$.ajax({
			type : "POST",
			async : false,
			data :{},
			url : "/LoginServlet/loginOut.action",
			contentType : "application/x-www-form-urlencoded; charset=utf-8",
			dataType : "json",
			success : function(rtnData){
				if(!isEmpty(rtnData)&&rtnData.data.status=="success"){
					location.href = "/html/public/login.html";
				} 
			}
		});
	}
	
	/**
	 * 初始化
	 */
	this.init = function() {
		
		// STEP-1 初始化对象
		this.topDiv = $('#topDiv');
		this.firstMenuDiv = $('#firstMenuDiv');
//		this.secondMenuDiv = $('#secondMenuDiv');
		this.menuPanelDiv = $('#menuPanelDiv');
		this.mainDiv = $('#mainDiv');
		
		// STEP-2 加载用户数据
		this.ajaxFindUserData();
		
		// STEP-3 进入portal首页
		this.gotoMainPage();
		
		// STEP-4 加载TopDiv
		this.writeTopDiv();
		
		// STEP-5 初始化一级菜单
		this.initFirstMenu();
		
		//定时检测鼠标是否移动 每3秒检测一次
//		this.intervalTime = setInterval(this.doInterval, 1000 * 3);
	}
	
	/**
	 * 首页鼠标移动事件
	 */
	this.doMouseOver = function() {
		var d = new Date();
		this.lastMoveTime = d.getTime();
	}
	
	/**
	 * 定时检查是否正在操作 
	 */
	this.doInterval = function() {
		var d = new Date();
		//如果超过30分钟没有操作，则弹出提示
		if ((d.getTime() - indexMenu.lastMoveTime) / 1000 > 30 * 60)
		{
			art.dialog.message('warning', '操作已经过期，请重新登录！', function(){
				indexMenu.logout();
			});
		}
	}
}


var indexMenu = new Index_menu();

$(document).ready(function(){
	indexMenu.init();
	
	/*$(document).mouseover(function(e) {
		indexMenu.doMouseOver();
	});*/
});

/**
 * 对外提供的打开首页中的菜单的公共方法
 * name：菜单名称；url：菜单的URL；
 */
function portalOpenMenu(name,url) {
	var menuObj = new Object();
	menuObj.title = name;
	menuObj.url = url;
	indexMenu.onloadChildSystemPage(menuObj);
}

/**
 * 对外提供的关闭首页中的菜单的公共方法
 * name：菜单名称
 */
function portalCloseMenu(name) {
	indexMenu.panelFrame.deleteLiFun(name);
}
/**
 * 检测页面是否打开
 * @param name ：页面名称 
 * @returns {Boolean} true：已打开 false未打开
 */
function isMenuOpen(name){
	var ttIndex = indexMenu.panelFrame.getFrameNumFun(name);
	if(null == ttIndex) {
		return false;
	}else{
		return true;
	}
}

function getCurrentLiTitle(){
   return indexMenu.panelFrame.getCurrentLiTitle(); 
}

